package com.office.shop.by.components;

import android.content.Context;
import android.content.SharedPreferences;
import com.office.shop.by.MultiAccActivity;

import java.util.zip.InflaterInputStream;

/**
 * Created by IntelliJ IDEA.
 * User: Олег Семенов
 * Date: 04.07.11
 * Time: 14:48
 * To change this template use File | Settings | File Templates.
 */
public class CAccounts {
    private static String sharedPreferencesTag = CConfig.mainPackage+".MultiAccActivity";

    public static  Context  Context  = null;
    public static  String[] logins = new String[] {"rabbit","avlukov","chirkov"};
    public static  String[] passwords = new String[] {"123456","123456","123456"};

    public static void init(Context context)
    {
       if(Context == null) Context = context;

       SharedPreferences sharedPreferences = Context.getSharedPreferences(CConfig.mainPackage+".MultiAccActivity", 0);
       String loginLine = sharedPreferences.getString("Logins","");
       String pwdLine = sharedPreferences.getString("Passwords","");

       if(loginLine!="")
       {
             logins = loginLine.split(CConfig.separatorDataSplit);
             passwords = pwdLine.split(CConfig.separatorDataSplit);
       }
    }

    public static void save(Context context)
    {
        if(Context == null) Context = context;
        SharedPreferences.Editor editor = Context.getSharedPreferences(CConfig.mainPackage+".MultiAccActivity", 0).edit();

        String pwdLine = "";
        String loginLine ="";

        if(CAccounts.logins.length > 1)
        {
            for(int i=0; i< CAccounts.logins.length; i++)
            {
               loginLine +=  CAccounts.logins[i] + CConfig.separatorData;
               pwdLine +=  CAccounts.passwords[i] + CConfig.separatorData;
            }

            editor.putString("Logins", loginLine.substring(0, loginLine.length()-1));
            editor.putString("Passwords",pwdLine.substring(0, pwdLine.length()-1));
            editor.commit();
        }
        else
        {
            editor.putString("Logins", "");
            editor.putString("Passwords","");
            editor.commit();
        }
    }

    public static void add(String login,String pwd)
    {
       if(login != "")
       {
                String[] newLogins = new  String[CAccounts.logins.length + 1];
                String[] newPwds = new  String[CAccounts.logins.length + 1];

                for (int i = 0; i < CAccounts.logins.length;i++)
                {
                    newLogins[i] = CAccounts.logins[i];
                    newPwds[i] = CAccounts.passwords[i];
                }
                newLogins[CAccounts.logins.length] = login;
                newPwds[CAccounts.logins.length] = pwd;

               CAccounts.logins =  newLogins;
               CAccounts.passwords =  newPwds;
       }
    }

    public static void delete(int id)
    {
          String[] newLogins = new  String[CAccounts.logins.length - 1];
          String[] newPwds = new  String[CAccounts.logins.length - 1];
          int j = 0;
          for (int i = 0; i < CAccounts.logins.length;i++)
          {
              if(i != id)
              {
                newLogins[j] = CAccounts.logins[i];
                newPwds[j] = CAccounts.passwords[i];
                j++;
              }
          }

          CAccounts.logins =  newLogins;
          CAccounts.passwords =  newPwds;
    }

    public static void edit(int id,String login,String pwd)
    {
         CAccounts.logins[id] = login;
         CAccounts.passwords[id] = pwd;
    }
}
